package perf.cli;

import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;

enum Optional {
	OPTIONAL( true ), MANDATORY( false );
	
	private final boolean val;
	Optional( boolean val ) {
		this.val = val;
	}
	
	public boolean val( ) {
		return this.val;
	}
}

public enum AppOpts {
	DATA_BASE( 'd', "Database to use", "file", Optional.OPTIONAL ),
	ADD_SETUP( 'A', "Add a new setup", "setup name", Optional.MANDATORY ),
	LIST_SETUP( 'L', "List setups" ),
	REMOVE_SETUP( 'R', "Removes a setup identified with it's index", "index", Optional.MANDATORY ),
	CHOOSE_SETUP( 'S', "Choose the setup to use", "index", Optional.MANDATORY ),
	ADD_RUN( 'a', "Add a new run", "file", Optional.MANDATORY ),
	LIST_RUN( 'l', "List runs"),
	REMOVE_RUN( 'r', "Remove a run", "index", Optional.MANDATORY),
	HELP( 'h', "print this help message" ),
	TEXT_DUMP( 't', "Dump database in text format", "run_index", Optional.OPTIONAL );
	
	private final char shortOpt;
	private final String help;
	private final Optional argOptional;
	private final String argName;
	
	AppOpts( char shortOpt, String help) {
		this.shortOpt = shortOpt;
		this.help = help;
		this.argName = null;
		this.argOptional = null;
	}
	
	AppOpts( char shortOpt, String help, String argName, Optional optional ) {
		this.shortOpt = shortOpt;
		this.help = help;
		this.argName = argName;
		this.argOptional = optional;		
	}
	
	public char shortOpt( ) {
		return shortOpt; 
	}
	
	public String help( ) {
		return help;
	}

	public Option getCLIOption( ) {
		Option opt = OptionBuilder.create( shortOpt );
		opt.setDescription( help );
		if ( argName != null ) {
			opt.setArgs(1);
			opt.setArgName( argName );
			opt.setOptionalArg( argOptional.val() );
		}
		return opt;
	}
	
	public static Options getOptions( ) {
		Options opts = new Options( );
		for ( AppOpts opt : AppOpts.values() ) {
			opts.addOption( opt.getCLIOption() );
		}
		return opts;
	}	
}
